import { Button, Tag, Image, Switch, Table } from 'antd'
import React, { useEffect, useState } from 'react'
import { getProListFn } from '../../api/pro'
import ExportJsonExcel from 'js-export-excel'
export default function Index() {

    const [proList, setproList] = useState([])
    useEffect(() => {
        getProListFn().then(res => {
            setproList(res.data)
        })
    }, [])



    const [current, setcurrent] = useState(1)
    const columns = [{
        title: '序号',
        render(t, r, i) {
            return <Tag>{(current - 1) * 10 + i + 1}</Tag>
        },
        width: 80
    }, {
        title: '商品名称',
        dataIndex: 'proname',
        width: 300
    }, {
        title: '商品图片',
        dataIndex: 'img1',
        width: 150,
        render(t) {
            return <Image src={t} width={80} />
        }
    }, {
        title: '商品分类',
        dataIndex: 'category',
    }, {
        title: '商品价格',
        dataIndex: 'originprice',
        sorter: (a, b) => a.originprice - b.originprice,
    }, {
        title: '商品折扣',
        dataIndex: 'discount',
        sorter: (a, b) => a.discount - b.discount,
    }, {
        title: '是否推荐',
        dataIndex: 'isrecommend',
        render(t, r) {
            return <Switch defaultChecked={t} />
        }
    }, {
        title: '是否售卖',
        dataIndex: 'issale',
        render(t) {
            return <Switch defaultChecked={t} />
        }
    }, {
        title: '是否秒杀',
        dataIndex: 'isseckill',
        render(t) {
            return <Switch defaultChecked={t} />
        }
    }
    ]


    // 配置导出内容
    function exportClick() {
        const option = {
            fileName: '小吴导出的文件',
            datas: [
                {
                    sheetData: proList,
                    sheetName: '工作表一',
                    sheetFilter: ['proname', 'proid'],
                    sheetHeader: ['商品名称', '商品 id'],
                    columnWidths: [20, 20],
                }, {
                    sheetData: proList,
                    sheetName: '工作表二',
                    sheetFilter: ['originprice', 'category'],
                    sheetHeader: ['商品价格', '商品类型'],
                    columnWidths: [20, 20],
                }
            ]
        }

        const toExcel = new ExportJsonExcel(option)

        toExcel.saveExcel()
    }

    return (
        <div>
            <h3>
                <Button onClick={exportClick}>文件导出</Button>
            </h3>



            <Table
                rowKey={'proid'}
                dataSource={proList}
                pagination={{
                    current,
                    onChange(page) {
                        setcurrent(page)
                    }
                }}
                columns={columns}
                scroll={{
                    y: 450
                }}
                locale={{
                    triggerDesc: '点击降序',
                    triggerAsc: '点击升序',
                    cancelSort: '取消排序',
                    filterConfirm: '确定',
                    filterReset: '重置',
                }}
            />

        </div>
    )
}
